#-*- coding:utf-8 -*-

# 找到需要翻译的资源文件

import xlrd
import os
import xml.dom.minidom
import xlwt
import re

LANGUAGE_TYPES = ['values-de-rDE', 'values-en-rUS', 'values-es-rES', 'values-fr-rFR', 'values-it-rIT', 'values-ar-rAE','values-ar-rEG',
                  'values-pt-rPT', 'values-ru-rRU', 'values-th-rTH', 'values-zh-rCN', 'values-ko-rKR', 'values-zh-rHK']

FILE_NAME = '英语母语校-对20181012.xlsx'
SAVE_NAME = '待翻译.xls'
NEED_NODES = ['string', 'item', 'country', 'timezone']
FILE_TYPE = ['strings.xml', 'strings_bt.xml', 'home_strings.xml', 'arrays.xml', 'string_arry.xml', 'string_arry.xml', 'countries.xml', 'timezones.xml']
SHEET_NAME = u'Sheet1'

zh_pattern = re.compile(u'[\u4e00-\u9fa5]+')


# 是否是中文
def contain_zh(word):
    word = word
    match = zh_pattern.search(word)
    return match


# 打开翻译资料库
def open_excel(file_name=FILE_NAME):
    # 当前文件的路径
    pwd = os.getcwd()
    # 当前文件的父路径
    father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
    file_name = os.path.join(father_path, file_name)
    try:
        file_handler = xlrd.open_workbook(file_name)
        return file_handler
    except Exception:
        print('打开文件失败')


# 打开sheet
def scan_excel(sheet_name1=SHEET_NAME):
    handler = open_excel()
    page = handler.sheet_by_name(sheet_name1)
    return page


# 读取指定列
def trim_cols(index=4):
    page = scan_excel()
    return page.col_values(index)


# 获取当前路径下所有FILE_TYPE文件中的包含中文的内容l
def get_all_string():
    work_dir = os.getcwd()
    values = []
    for parent, dir_names, file_names in os.walk(work_dir):
        for file_name in file_names:
            # 过滤出FILE_TYPE
            if file_name in FILE_TYPE:
                # 完整文件路径
                file_path = os.path.join(parent, file_name)
                root_node = xml.dom.minidom.parse(file_path).documentElement
                # 遍历指定的节点
                for need_node in NEED_NODES:
                    string_nodes = root_node.getElementsByTagName(need_node)
                    for string_node in string_nodes:
                        value = string_node.childNodes[0].nodeValue
                        if value is None:
                            # 值为none
                            pass
                        elif contain_zh(value):
                            # 包含中文
                            values.append(value)
                            pass
    # 去重
    values = list(set(values))
    return values


# 寻找未翻译的资源文件
def find_values():
    all_strings = get_all_string()
    exist_string = trim_cols()
    res_list = []
    for item in all_strings:
        # 如果没有该资源 则加入需要翻译的List中
        find = False
        for tmp_exist_string in exist_string:
            if tmp_exist_string.lower().strip() == item.lower().strip():
                find = True
        if not find:
            res_list.append(item)
    return res_list


# 样式
def set_style(name, height, bold=False):
    style = xlwt.XFStyle()
    font = xlwt.Font()
    font.name = name
    font.bold = bold
    font.color_index = 4
    font.height = height
    return style


# 写excel
def create_values():
    res_list = find_values()
    # 创建工作簿
    f = xlwt.Workbook()
    # 创建sheet
    sheet1 = f.add_sheet(SHEET_NAME, cell_overwrite_ok=True)
    # 设置样式
    style = set_style('Times New Roman', 220, True)
    row = 0
    for item in res_list:
        row += 1
        sheet1.write(row, 0, item, style)
    f.save(SAVE_NAME)


if __name__ == "__main__":

    create_values()
